-- STRINGS = GLOBAL.STRINGS
-- RECIPETABS = GLOBAL.RECIPETABS
-- Recipe = GLOBAL.Recipe
-- Ingredient = GLOBAL.Ingredient
-- TECH = GLOBAL.TECH
-- TheWorld = GLOBAL.TheWorld
-- AllPlayers = GLOBAL.AllPlayers
-- AllRecipes = GLOBAL.AllRecipes
-- TheSim = GLOBAL.TheSim
GLOBAL.setmetatable(env,{__index=function(t,k) return GLOBAL.rawget(GLOBAL,k) end})
----------------------------------------------------------------------------------------------------------------------------------------
----- 语言设置Fn 放这里，方便外部API修改
-- type( TUNING.__NPC_LANGUAGE ) == function or string
TUNING.__NPC_LANGUAGE = TUNING.__NPC_LANGUAGE or function()
	-- return "en"
	local language = "en"
	pcall(function()
		language = TUNING.NPC_CONFIG.LANGUAGE
		if language == "auto" then
			if LOC.GetLanguage() == LANGUAGE.CHINESE_S or LOC.GetLanguage() == LANGUAGE.CHINESE_S_RAIL or LOC.GetLanguage() == LANGUAGE.CHINESE_T then
				language = "ch"
			elseif  LOC.GetLanguage() == LANGUAGE.JAPANESE then
				language = "jp"
			else
				language = "en"
			end
		end                
	end)
	return language
end
----------------------------------------------------------------------------------------------------------------------------------------


Assets = {
}
modimport("imports/__All_imports_init.lua")	---- 所有 import 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 为保证本MOD 的良好顺畅体验，屏蔽掉一些容易造成崩溃的MOD，或者检测到不利于本MOD运行的情况
			--- 遇到有冲突的MOD，则不加载本MOD
			local temp_this_mod_name = modname
			local mod_info = KnownModIndex:GetModInfo(temp_this_mod_name) or {}
			local mod_display_name = mod_info.name or ""


			local need_2_block = false
			local blocked_str = ""
			local block_check_pass__flag = false	--- 只检查一次，不用每次进出洞穴都检查
			local function Quit_The_Game()		--- 温和的退出游戏
					if TheWorld and TheWorld.ismastersim and TheNet:IsDedicated() then
						TheSim:Quit()   --- 有洞穴的存档
					else
						----------- 没洞穴的存档
						-- TheSim:Reset()	--- 重置程序,会重载存档。
						-- TheNet:Disconnect(true)	--- 会游戏断开并卡在一个画面。

						-- DoRestart(false)

						--- 代码复制自 DoRestart
						PerformingRestart = true
						Settings.match_results = {}
						ShowLoading()
						TheFrontEnd:Fade(false, 1,function()
							TheNet:Disconnect(true)
							EnableAllMenuDLC()							
							if TheNet:GetIsHosting() then
								TheSystemService:StopDedicatedServers(not IsDynamicCloudShutdown)
							end							
							StartNextInstance()
							inGamePlay = false
							PerformingRestart = false
						end)
					end
			end
			local function Get_Blocked_Reason(index)	--- 原因
				local language = nil
				if type(TUNING.__NPC_LANGUAGE) == "function" then
					language = TUNING.__NPC_LANGUAGE()
				elseif type(TUNING.__NPC_LANGUAGE) == "string" then
					language = TUNING.__NPC_LANGUAGE
				end
				local annouce_table = TUNING.NPC_ANNOUNCE_TABLE[language]
				if annouce_table == nil then
					annouce_table = TUNING.NPC_ANNOUNCE_TABLE["ch"]
				end
				if annouce_table == nil then
					return "Game Quit With No Reason"
				end
				local blocked_reasons = annouce_table["npc_mod_blocked_reason"] or {}
				if blocked_reasons[tostring(index)] then
					return blocked_reasons[tostring(index)]
				else
					return "Game Quit With No Reason"
				end
			end
			
			local function block_by_workshop_id()			---- 靠工坊ID 屏蔽	
				local loaded_modnames = ModManager:GetEnabledModNames() or {}
				local block_mod_floder_name = {
					["workshop-1699194522"] = true,		-- steam ID
					["workshop-1991746508"] = true,		-- steam ID
				}

				for k, temp in pairs(loaded_modnames) do
					if temp and block_mod_floder_name[temp] then
						print("npc mod error : Loading with mods on the block list")
						local str = mod_display_name .. " : ".. tostring(Get_Blocked_Reason("mod_blocked")) .. "  ".. temp
						return true,str
					end
				end
				return false,""
			end			

			local function block_by_prefab_loaded()	---- 屏蔽指定prefab
				---- 加载了某些prefab
					local prefab_check_list = {"fangcunhill","pigsy","monkey_king","book_myth"}
					for k,prefab_name  in pairs(prefab_check_list) do
						if PrefabExists(tostring(prefab_name)) then
							print("npc mod error : Loading with the mod in the blocked list")							
							local str = mod_display_name .. " : ".. tostring(Get_Blocked_Reason("prefab_block"))
							if STRINGS.NAMES[string.upper(prefab_name)] then
								str = str .. STRINGS.NAMES[string.upper(prefab_name)]
							else
								str = str .. "NONE"
							end
							return true,str
						end
					end
				return false,""
			end

			if not need_2_block then	--- 靠 工坊id 检查
				need_2_block , blocked_str = block_by_workshop_id()
			end
			-- if not need_2_block then	--- 加载期间检查，玩家载入后再检查
			-- 	need_2_block , blocked_str = block_by_prefab_loaded()
			-- end

			AddPlayerPostInit(function(player_inst)
				if not TheWorld.ismastersim then
					return
				end
				if block_check_pass__flag then
					-- print("info :pass the block check")
					return
				end
				player_inst:DoTaskInTime(10,function()
					
							if not need_2_block then	--- 靠 工坊id 检查
								need_2_block , blocked_str = block_by_workshop_id()
							end
							if not need_2_block then	--- 加载期间检查，玩家载入后再检查
								need_2_block , blocked_str = block_by_prefab_loaded()
							end

							if need_2_block then
								local temp_inst = CreateEntity()
								temp_inst:DoPeriodicTask(1,function()
									TheNet:SystemMessage(blocked_str)							
								end)
								temp_inst:DoTaskInTime(15,function()
									Quit_The_Game()
								end)
							else
								block_check_pass__flag = true
							end
				end)
			end)

			------ 终止后续MOD加载
			if need_2_block == true then
				return
			end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





if modname == "NPCs" then -- 如果文件夹名字是 "NPCs"
	TUNING.NPC_MOD_TEST_DEBUG = true	
else
	--- 本MOD 的ID ：steam ： workshop-2486802405
	TUNING.NPC_MOD_TEST_DEBUG = false	
	
end

PrefabFiles =
{
	"npcdebugstone",
	"the_npc_spells",

	"the_npc_characters",
	"the_npc_events",
	"the_npc_fx",
	"the_npc_items",
	"the_npc_projectiles",
	"the_npc_cards"
	

}

modimport("Original_Prefabs_Upgrade_for_npc/_All_Original_Prefabs_Upgrade_Init.lua")	---- 升级饥荒原有物品，改进功能
modimport("Original_Components_Upgrade_for_npc/_All_Original_Components_Upgrade_Init.lua")	---- 升级饥荒原有组件，改进功能
modimport("Original_AnimState_Upgrade_for_npc/_All_Original_AnimState_Upgrade_Init.lua")	---- 升级饥荒原有动画组件，改进功能
modimport("Original_Widgets_Upgrade_for_npc/_All_Original_Widgets_Upgrade_for_npc_Init.lua")	---- 升级饥荒原有界面组件，改进功能

modimport("test_fn/_Load_All_debug_fn.lua")	---- 载入测试用的模块



AddPlayerPostInit(function(inst)
        if not GLOBAL.TheWorld.ismastersim then
            return
        end
		
		if inst.components.npc_base_lib == nil then
			inst:AddComponent("npc_base_lib")
		end
		if inst.components.npc_everything_data == nil then
			inst:AddComponent("npc_everything_data")
		end

		if modname == "NPCs" then -- 如果文件夹名字是 "NPCs"
			-- GLOBAL.PRINT_SOURCE = true -- 在打印时显示文件行数
			print("Warning : MOD 正在开发,不运行NPC定时刷新器")
			TUNING.NPC_MOD_TEST_DEBUG = true
			return
		end

		-- inst:DoTaskInTime(0.5,function()
		-- 	if inst.components.playercontroller and not inst:HasTag("ALL_NPC_EVENT_TASK_INIT_FINISH") then
		-- 		inst.npc_base_lib:RPC_AddTags({"ALL_NPC_EVENT_TASK_INIT_FINISH"})
		-- 		inst.components.npc_base_lib:ALL_NPC_EVENT_TASK_INIT()
		-- 		inst:AddTag("ALL_NPC_EVENT_TASK_INIT_FINISH")
		-- 		print(inst,"ALL_NPC_EVENT_TASK_INIT_FINISH")
		-- 	else
		-- 		print(inst,"是个非玩家角色，终止行动任务。")
		-- 	end
		-- end)

		----- 尝试兼容其他MOD和节省Tag位置
		inst:DoTaskInTime(0.5,function()
			------------------------------------------
			--- 其他MOD兼容跳出
				local function other_mods_checker()
					if inst:HasTag("mym_mate") then  --- 尝试兼容 的队友MOD
						return true
					end
				end
				if other_mods_checker() then
					return
				end
			------------------------------------------
			if inst.components.playercontroller and inst.userid and not inst.ALL_NPC_EVENT_TASK_INIT_FINISH then
				inst.components.npc_base_lib:ALL_NPC_EVENT_TASK_INIT()
				inst.ALL_NPC_EVENT_TASK_INIT_FINISH = true
				print(inst,"ALL_NPC_EVENT_TASK_INIT_FINISH")
			else
				print(inst,"是个非玩家角色，终止行动任务。")
			end
		end)
		
    end
)


-- dofile(resolvefilepath("test_fn/test.lua"))